package com.gmako.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gmako.dao.BaseDAO;
import com.gmako.entity.Businessimg;
import com.gmako.entity.Businessinfo;
import com.gmako.entity.Packageinfo;
import com.gmako.entity.Packimg;
import com.gmako.entity.Userinfo;
import com.gmako.service.BusinessinfoService;
import com.gmako.service.UserinfoService;
import com.gmako.utils.Pager;

@Service
public class BusinessinfoServiceImpl implements BusinessinfoService{
	@Autowired
	private BaseDAO baseDAO;

	/**
	 * 添加商户信息
	 * @author Warlock
	 * @param houseInfo
	 */
	
	public void addBusinessInfo(Businessinfo businessinfo) {
		System.out.println("添加图片");
		baseDAO.save(businessinfo);
	}
	
	/**
	 * 修改商户信息
	 * @author Warlock
	 * @param houseInfo
	 */
	
	public void updateBusinessInfo(Businessinfo businessinfo) {
		baseDAO.update(businessinfo);
	}
	
	/**
	 * 添加详细图片
	 * @author Warlock
	 * @param informationimage
	 * @return
	 */
	public void save(Businessimg businessimg) {
		baseDAO.save(businessimg);
		
	}
	
	/**
	 * 删除详细图片
	 * @author Warlock
	 * @param informationimage
	 * @return
	 */
	public void delete(Businessimg businessimg,int id){
		//查询数据库中是否有信息
		List list = baseDAO.findByProperty("Businessimg","id", id);	
		if(list.size()!=0){
			System.out.println("数据库中有图片");
			baseDAO.deleteById(Businessimg.class, id);
		}else{
			System.out.println("数据库中无图片");
		}
		
	}

	
	
	/**
	 * 商家列表页，根据商家ID 查询相应套餐
	 * @author Warlock
	 * @param bid
	 * @return
	 */
	public Pager findPackInfoByBid(int pageSize, int pageNo,int bid) {	
		String sqlCount ="select count(*) from Packageinfo where businessInfoId="+bid;
		String SqlList ="select * from Packageinfo where businessInfoId="+bid;
		//根据套餐ID 查询图片取 第一个
		Pager p = baseDAO.findPagerSql(pageSize, pageNo, sqlCount, SqlList, Packageinfo.class);
		List<Packageinfo> li = p.getResultList();
		Packageinfo  packageinfo = new Packageinfo();
		List<Packimg> packimg = null;
		for (int i =0;i<li.size();i++) {
			packimg=baseDAO.findByProperty("Packimg","packageId", li.get(i).getId());
			packageinfo = li.get(i);
			packageinfo.setImgFirst(packimg.get(0).getImgName());
		}
		p.setResultList(li);
		return p;
	}
	
	/**
	 * 根据商家ID 查询商家信息
	 * @author Warlock
	 * @param bid
	 * @return
	 */
	public Businessinfo findBusinessInfoByBid(int bid) {
		String hql = "from Businessinfo where id="+bid;
		return (Businessinfo)baseDAO.loadObject(hql);
	}	
	
	/**
	 * 根据商家UID  查询商家信息
	 * @author Warlock
	 * @param bid
	 * @return
	 */
	public Businessinfo findBusinessInfoByUid(int uid){
		String hql = "from Businessinfo where uid="+uid;
		return (Businessinfo)baseDAO.loadObject(hql);
	}
	
	
	

	/**
	 * 根据商家类型 查询商家信息
	 * @author Warlock
	 * @param bid
	 * @return
	 */
	public List<Businessinfo> findBusinessNameByType(Class clazz,String propertyName,Object value){
			return baseDAO.findByProperty(clazz.getName(),propertyName, value);	
	}
	/**
	 * 根据ID 查询商家图片
	 * @author Warlock
	 * @param bid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Businessimg> findBusinessImgs(int bid) {
		return baseDAO.findByProperty("Businessimg","bid", bid);	
	}	
	
	/**
	 * 团购
	 * @author Warlock
	 * @param bid
	 * @return
	 */
	public Pager findPagerHql(final int pageSize, final int pageNo,int type){
		 String sqlCount=null,SqlList=null;
		 if (type==1) {
			 sqlCount="select count(*) from Packageinfo where PackType = 2";
			 SqlList="select * from Packageinfo where PackType = 2 order by zhekou desc";
		}
		 if (type==2) {
			 sqlCount="select count(*) from Packageinfo where PackType = 2";
			 SqlList="select * from Packageinfo where PackType = 2 order by zhekou asc";
		 }
		 if (type==3) {
			 sqlCount="select count(*) from Packageinfo where PackType = 2";
			 SqlList="select * from Packageinfo where PackType = 2 order by startTime desc";
		 }
		 if (type==4) {
			 sqlCount="select count(*) from Packageinfo where PackType = 2";
			 SqlList="select * from Packageinfo where PackType = 2 order by activityStart desc";
		 }
		 Pager p = baseDAO.findPagerSql(pageSize, pageNo, sqlCount, SqlList, Packageinfo.class);
			List<Packageinfo> li = p.getResultList();
//			 System.out.println("查出的P大小"+li.size());
			Packageinfo  packageinfo = new Packageinfo();
			List<Packimg> packimg = null;
			for (int i =0;i<li.size();i++) {
				packimg=baseDAO.findByProperty("Packimg","packageId", li.get(i).getId());
				packageinfo = li.get(i);
				packageinfo.setImgFirst(packimg.get(0).getImgName());
			}
			p.setResultList(li);
			return p;
	}

	

}
